"""
文件名：Base.py
作用：对selenium进行二次封装
    1. 打开浏览器(单独封装)
    2. 访问网址
    3. 元素定位
    4. 元素操作
    5. 控件操作
"""
# 导入需要的包
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# 打开浏览器
def open_browser(browser="chrome"):
    """
    通过浏览器名称，打开对应的浏览器
    :param browser:浏览器名称
    :return:driver/None
    :lower():将字符串转换为小写
    """
    if browser.lower() == "chrome":
        return webdriver.Chrome()
    elif browser.lower() == "firefox":
        return webdriver.Firefox()
    elif browser.lower() == "edge":
        return webdriver.Edge()
    else:
        print("请输入正确的浏览器名称，例如：Chrome、Firefox、Edge")
        return None


# 基类、超类
class Base(object):
    def __init__(self, driver):
        """
        接收浏览器对象
        :param driver: open_browser()
        """
        self._driver = driver

    def get_url(self, url):
        """
        访问网址
        :param url: 网址链接：http://www.xxxx.com
        :return:
        """
        if url.startswith("http://") or url.startswith("https://"):
            self._driver.maximize_window()
            self._driver.get(url)
        else:
            print("请输入正确格式的网址")

    def _find_element(self, locator: tuple, timeout=10):
        """
        使用WebDriverWait和EC模块，对元素定位进行二次封装
        :param locator: 定位器，(By.ID,"ID属性值")
        :param timeout: 最长等待时间
        :return: "WebElement"/False
        """
        try:
            return WebDriverWait(self._driver, timeout).until(EC.presence_of_element_located(locator))
        except:
            print(f"元素{locator}没有找到")
            return False

    def _find_elements(self, locator: tuple, timeout=10):
        """
        使用WebDriverWait和EC模块，对元素定位多个元素进行二次封装
        :param locator: 定位器，(By.ID,"ID属性值")
        :param timeout: 最长等待时间
        :return: list/False  例：[WebElement1,WebElement2...]
        """
        try:
            return WebDriverWait(self._driver, timeout).until(EC.presence_of_all_elements_located(locator))
        except:
            print(f"元素{locator}没有找到")
            return False

    def click(self, locator):
        try:
            self._find_element(locator).click()
        except:
            print(f"元素{locator}没有找到，无法执行点击操作")

    def send_keys(self, locator, text: str):
        try:
            self._find_element(locator).send_keys(text)
        except:
            print(f"元素{locator}没有找到，无法执行输入操作")

    def clear(self, locator):
        try:
            self._find_element(locator).clear()
        except:
            print(f"元素{locator}没有找到，无法执行清空操作")

    def get_element_text(self, locator):
        try:
            return self._find_element(locator).text
        except:
            print(f"元素{locator}没有找到，无法获取文本")
            return False

    def is_text_equal_element(self, locator, text):
        """
        判断获取到的文本是否和预期的文本一致
        :param locator: 定位器
        :param text: 输入的文本
        :return: True/False
        """
        result_text = self.get_element_text(locator)
        if result_text is not False:
            return result_text == text

    def close_browser(self):
        """
        关闭浏览器
        :return:
        """
        self._driver.quit()


if __name__ == '__main__':
    base = Base(open_browser())
    base.get_url("http://www.baidu.com")
    sleep(3)
    base.send_keys((By.ID, "kw"), "selenium封装")
    sleep(3)
    base.click((By.ID, "su"))
    sleep(3)
    base.close_browser()
